package com.chengxiaoxiao.dao.impl;

import com.chengxiaoxiao.dao.ClassesDao;
import com.chengxiaoxiao.dao.UsersDao;
import com.chengxiaoxiao.domain.BlackList;
import com.chengxiaoxiao.domain.Classes;
import com.chengxiaoxiao.domain.Users;
import com.chengxiaoxiao.domain.UsersExtra;
import com.chengxiaoxiao.utils.MD5Util;
import com.chengxiaoxiao.utils.db.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UsersDaoImpl implements UsersDao {
    private DBAssist da = new DBAssist(DBCPUtil.getDataSource());
    private ClassesDao classesDao = new ClassesDaoImpl();


    @Override
    public void add(Users user) {
        String sql =
                "insert into users(username,`password`,realname,gender,classesid,studentNum,isBlack,mostBorrowNum,picture) " +
                        "value(?,?,?,?,?,?,?,?,?)";

        da.update(sql, user.getUserName(), MD5Util.encode(user.getPassword()), user.getRealName(), user.getGender(), user.getClassesId(), user.getStudentNum(), user.getIsBlack(), user.getMostBorrowNum(), user.getPicture());
    }

    @Override
    public void update(Users user) {
        String sql =
                "update users set username=?,`password`=?,realName=?,gender=?,classesId=?,studentNum=?,isBlack=?,mostBorrowNum=?,picture=? where id=?";

        da.update(sql, user.getUserName(), MD5Util.encode(user.getPassword()), user.getRealName(), user.getGender(), user.getClassesId(), user.getStudentNum(), user.getIsBlack(), user.getMostBorrowNum(), user.getPicture(), user.getId());

    }

    @Override
    public void delete(long id) {
        String sql = "delete from users where id=?";
        da.update(sql, id);
    }

    @Override
    public List<Users> get() {
        List list = (List) da.query("select * from users", new BeanListHandler(Users.class));
        return list;
    }

    @Override
    public Users getbyId(long id) {
        Users user = getUserAndExtra(id);
        Classes cla = classesDao.getbyId(user.getClassesId());
        if (cla.getCollegeId() != 0) {
            cla.setClasses(classesDao.getbyId(cla.getCollegeId()));
        }
        user.setClasses(cla);

        return user;
    }

    @Override
    public List getListPage(int startIndex, int pageSize) {
        String sql = "select * from users where isBlack=0 limit ?,?";
        List<Users> list = (List) da.query(sql, new BeanListHandler(Users.class), startIndex, pageSize);

        List<Users> model = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Users user = list.get(i);
            Users users = getUserAndExtra(user.getId());

            Classes cla = classesDao.getbyId(users.getClassesId());
            if (cla.getCollegeId() != 0) {
                cla.setClasses(classesDao.getbyId(cla.getCollegeId()));
            }
            users.setClasses(cla);

            model.add(users);
        }

        return model;
    }


    @Override
    public int getTotalRecordsNum() {
        return (int) da.query("select COUNT(*) from users", new ScalarHandler());
    }

    @Override
    public Users getUserByUsernameAndPassword(String username, String password) {

        String sql = "select * from users where username=? and password=?";

        password =MD5Util.encode( password);
        return (Users) da.query(sql, new BeanHandler(Users.class), username,password);

    }

    @Override
    public Users getUserAndExtra(long id) {
        String sql = "select * from users where id=?";
        Users user = (Users) da.query(sql, new BeanHandler(Users.class), id);

        if (user == null) {
            return null;
        }

        String sql1 = "select * from usersextra where userId=?";
        UsersExtra usersExtra = (UsersExtra) da.query(sql1, new BeanHandler(UsersExtra.class), user.getId());

        user.setUsersExtra(usersExtra);

        return user;
    }

    @Override
    public List getPageLikeSNum(int startIndex, int pageSize, String snum) {

        String sql = "select * from users where isBlack=0 and studentNum like? limit ?,?";
        List<Users> list = (List) da.query(sql, new BeanListHandler(Users.class), startIndex, pageSize, "%" + snum + "%");

        List<Users> model = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Users user = list.get(i);
            Users users = getUserAndExtra(user.getId());

            Classes cla = classesDao.getbyId(users.getClassesId());
            if (cla.getCollegeId() != 0) {
                cla.setClasses(classesDao.getbyId(cla.getCollegeId()));
            }
            users.setClasses(cla);

            model.add(users);
        }

        return model;

    }

    @Override
    public Users getAllUserInfo(int ids) {
        String sql = "select * from users where id=?";
        Users user = (Users) da.query(sql, new BeanHandler(Users.class), ids);

        if (user == null) {
            return null;
        }

        String sql1 = "select * from usersextra where userId=?";
        UsersExtra usersExtra = (UsersExtra) da.query(sql1, new BeanHandler(UsersExtra.class), user.getId());

        user.setUsersExtra(usersExtra);

        Classes cla = classesDao.getbyId(user.getClassesId());
        if (cla.getCollegeId() != 0) {
            cla.setClasses(classesDao.getbyId(cla.getCollegeId()));
        }
        user.setClasses(cla);

        return user;

    }

    @Override
    public void black(int ids, String cause) {
        Users users = getbyId(ids);
        users.setIsBlack(1);

        update(users);

        BlackList blackList = new BlackList();
        blackList.setUsers_Id(users.getId());
        blackList.setSubTime(new Date());
        blackList.setCause(cause);
        da.update("insert into blacklist(cause,subTime,users_id) values(?,?,?)", blackList.getCause(), blackList.getSubTime(), blackList.getUsers_Id());
    }

    @Override
    public Users getUserInfoByUid(String uid) {

        String sql = "select * from users where userName=?";
        Users user = (Users) da.query(sql, new BeanHandler(Users.class), uid);

        if (user == null) {
            return null;
        }

        String sql1 = "select * from usersextra where userId=?";
        UsersExtra usersExtra = (UsersExtra) da.query(sql1, new BeanHandler(UsersExtra.class), user.getId());

        user.setUsersExtra(usersExtra);

        Classes cla = classesDao.getbyId(user.getClassesId());
        if (cla.getCollegeId() != 0) {
            cla.setClasses(classesDao.getbyId(cla.getCollegeId()));
        }
        user.setClasses(cla);

        return user;
    }
}
